package com.newtype.meeting.client;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.newtype.meeting.shared.frequencySource.DateSeq;
import com.newtype.meeting.shared.frequencySource.DateValue;
import com.newtype.meeting.shared.frequencySource.DateWeek;
import com.newtype.meeting.shared.frequencySource.Frequency;

/**
 * 事件类型数据定义类
 * @author 汪恭泽
 *
 */
public class FrequencySourceData {
	/**
	 * 周期数据
	 * @return
	 */
	public static List<Frequency> getFrequencies() {
		List<Frequency> frequencies = new ArrayList<Frequency>();
		frequencies.add(new Frequency("0", "单一日期"));
		frequencies.add(new Frequency("1", "每周"));
		frequencies.add(new Frequency("2", "每月"));
		return frequencies;
	}

	/**
	 * 星期数据
	 * @return
	 */
	public static List<DateWeek> getDateWeeks() {
		List<DateWeek> dateWeeks = new ArrayList<DateWeek>();
		dateWeeks.add(new DateWeek("0", "星期日"));
		dateWeeks.add(new DateWeek("1", "星期一"));
		dateWeeks.add(new DateWeek("2", "星期二"));
		dateWeeks.add(new DateWeek("3", "星期三"));
		dateWeeks.add(new DateWeek("4", "星期四"));
		dateWeeks.add(new DateWeek("5", "星期五"));
		dateWeeks.add(new DateWeek("6", "星期六"));
		return dateWeeks;
	}

	/**
	 * 频率数据
	 * @return
	 */
	public static List<DateSeq> getDateSeqs() {
		List<DateSeq> dateSeqs = new ArrayList<DateSeq>();
		dateSeqs.add(new DateSeq("100", "固定日期"));
		for (int i = 1; i < 6; i++) {
			dateSeqs.add(new DateSeq(i + "", "第" + i + "个"));
		}
		dateSeqs.add(new DateSeq("0", "最后1个"));
		return dateSeqs;
	}

	/**
	 * 日期数据
	 * @return
	 */
	public static List<DateValue> getDateValues() {
		List<DateValue> dateValues = new ArrayList<DateValue>();
		for (int i = 1; i <= 31; i++) {
			dateValues.add(new DateValue(i + "", i + "日"));
		}
		return dateValues;
	}

	/**
	 * 获取周期对象
	 * @param frequency
	 * @return
	 */
	public static Frequency getFrequency(String frequency) {
		List<Frequency> fs = FrequencySourceData.getFrequencies();
		Frequency fre = null;
		for (Frequency f : fs) {
			if (f.getFrequency().equals(frequency)) {
				fre = new Frequency(f.getFrequency(), f.getShowFrequency());
			}
		}
		return fre;
	}

	/**
	 * 获取星期对象
	 * @param dateweek
	 * @return
	 */
	public static DateWeek getDateWeek(String dateweek) {
		List<DateWeek> ws = FrequencySourceData.getDateWeeks();
		DateWeek week = null;
		for (DateWeek w : ws) {
			if (w.getDateWeek().equals(dateweek)) {
				week = new DateWeek(w.getDateWeek(), w.getShowDateWeek());
			}
		}
		return week;
	}

	/**
	 * 获取频率对象
	 * @param dateseq
	 * @return
	 */
	public static DateSeq getDateSeq(String dateseq) {
		List<DateSeq> ss = FrequencySourceData.getDateSeqs();
		DateSeq seq = null;
		for (DateSeq s : ss) {
			if (s.getDateSeq().equals(dateseq)) {
				seq = new DateSeq(s.getDateSeq(), s.getShowDateSeq());
			}
		}
		return seq;
	}

	/**
	 * 获取日期对象
	 * @param datevalue
	 * @return
	 */
	public static DateValue getDateValue(String datevalue) {
		List<DateValue> vs = FrequencySourceData.getDateValues();
		DateValue value = null;
		for (DateValue v : vs) {
			if (v.getDateValue().equals(datevalue)) {
				value = new DateValue(v.getDateValue(), v.getShowDateValue());
			}
		}
		return value;
	}

	/**
	 * 计算出一段时间内是星期几的日期
	 * @param startDate 开始日期
	 * @param endDate	结束日期
	 * @param dayOfWeek 星期几
	 */
	@SuppressWarnings("deprecation")
	public static List<Date> getDayOfWeek(Date startDate, Date endDate,
			int dayOfWeek) {
		List<Date> strDays = new ArrayList<Date>();
		//获取开始日期是星期几
		int weekDay = startDate.getDay();

		long day = 86400000;//一天的millis
		long mod = (endDate.getTime() - startDate.getTime()) / day;//两个日期相差天数
		int week = (int) (mod / 7 + 1);//计算有多少个星期

		int differ = dayOfWeek - weekDay;// 获取相差的天数
		Date tmpDate = new Date(startDate.getTime() + differ * day);

		Date strDate = new Date();
		strDate = tmpDate;
		if (tmpDate.compareTo(startDate) >= 0
				&& tmpDate.compareTo(endDate) <= 0) {
			strDays.add(strDate);
		}
		for (int i = 0; i < week; i++) {
			tmpDate = new Date(tmpDate.getTime() + day * 7);
			strDate = tmpDate;
			if (tmpDate.compareTo(startDate) >= 0
					&& tmpDate.compareTo(endDate) <= 0)
				strDays.add(strDate);
		}
		return strDays;
	}

	/**
	 * 每月指定日期
	 * @param startDate 开始日期
	 * @param endDate   结束日期
	 * @param dayOfMonth 指定日期
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static List<Date> getDayOfMonth(Date startDate, Date endDate,
			int dayOfMonth) {
		List<Date> strDays = new ArrayList<Date>();
		//计算出开始到结束的所有日期
		long day = 86400000;//一天的millis
		long mod = (endDate.getTime() - startDate.getTime()) / day;//两个日期相差天数

		Date strDate = new Date();
		Date tmpDate = new Date(startDate.getTime());
		strDate = tmpDate;
		if (tmpDate.compareTo(startDate) >= 0
				&& tmpDate.compareTo(endDate) <= 0
				&& tmpDate.getDate() == dayOfMonth) {
			strDays.add(strDate);
		}
		for (int i = 0; i < mod; i++) {
			tmpDate = new Date(tmpDate.getTime() + day);
			strDate = tmpDate;
			if (tmpDate.compareTo(startDate) >= 0
					&& tmpDate.compareTo(endDate) <= 0
					&& tmpDate.getDate() == dayOfMonth)
				strDays.add(strDate);
		}
		return strDays;
	}

	/**
	 * 计算范围内每月第几个星期几
	 * @param startDate 开始日期
	 * @param endDate 结束日期
	 * @param weekDayOfMonth 第几个
	 * @param dayOfWeek 星期几
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static List<Date> getWeekDayOfMonth(Date startDate, Date endDate,
			int weekDayOfMonth, int dayOfWeek) {
		// 存放所有星期几的日期
		List<Date> weekDayList = getDayOfWeek(startDate, endDate, dayOfWeek);
		//存放所有星期几的日期
		List<Date> weekDayAllList = getDayOfWeek(startOfMonth(startDate),
				endOfMonth(endDate), dayOfWeek);

		// 存放年份与月份
		Set<String> monthSet = new HashSet<String>();
		for (Date wd : weekDayList) {
			String str = String.valueOf((wd.getYear() + 1900))
					+ (wd.getMonth() + 1);
			monthSet.add(str);
		}

		//封装开始日期到结束日期的星期几结果
		List<List<Date>> listAll = new ArrayList<List<Date>>();
		for (String str : monthSet) {
			if (str.length() == 5 && !str.endsWith("0") || str.length() > 5) {
				List<Date> ymList = new ArrayList<Date>();
				for (Date weekDay : weekDayAllList) {
					String strDate = String.valueOf((weekDay.getYear() + 1900))
							+ (weekDay.getMonth() + 1);
					if (str.equals(strDate)) {
						ymList.add(weekDay);
					}
				}
				listAll.add(ymList);
			}
		}
		//存放每月第几个星期几
		List<Date> dateList = new ArrayList<Date>();
		for (List<Date> list : listAll) {
			int index = 0;
			if (weekDayOfMonth == 0) {
				index = list.size();
			} else if (weekDayOfMonth < list.size()) {
				index = weekDayOfMonth;
			}
			if (index == 0) {
				break;
			} else {
				dateList.add(list.get(index - 1));
			}
		}
		//存放结果日期
		List<Date> resultList = new ArrayList<Date>();
		for (Date date1 : weekDayList) {
			for (Date date2 : dateList) {
				if (date1.equals(date2)) {
					resultList.add(date1);
				}
			}
		}
		return resultList;
	}

	/** 
	 * 功能：一个月的第一天 
	 * @param time 
	 * @return 
	 */
	@SuppressWarnings("deprecation")
	public static Date startOfMonth(Date date) {
		date.setDate(1);
		date.setHours(0);
		date.setMinutes(0);
		date.setSeconds(0);
		return date;
	}

	/** 
	 * 功能: 一月中的最后一天. 
	 * 输入: time = 给出的当周内的某一天. 
	 *  输出: 该周的最后一天. 
	 * @param time 
	 * @return 
	 */
	@SuppressWarnings("deprecation")
	public static Date endOfMonth(Date date) {
		date.setDate(DaysInMonth(date));
		return date;
	}

	/** 
	 * 功能: 判断是否是闰年. 
	 * 输入: year = 给出的 1582 年以后的年份 
	 * 输出: TRUE 是闰年, FALSE 不是.. 
	 * 
	 * 能被100整除, 不能被400整除的年份, 不是闰年. 
	 * 能被100整除, 也能被400整除的年份, 是闰年. 
	 * 不能被100整除, 能被4整除的年份是闰年. 
	 * @param year 
	 * @return 
	 */
	public static boolean IsLeapYear(int year) {
		if ((year % 100) == 0)
			return ((year % 400) == 0);
		return ((year % 4) == 0);
	}

	/** 
	 * 功能: 当月的天数. 
	 * 输入: month = 给出的该月的某一个时间. 
	 * 输出:  这个月有多少天. 
	 * 
	 * @param date 
	 * @return 
	 */
	@SuppressWarnings("deprecation")
	public static int DaysInMonth(Date date) {
		int month = date.getMonth();
		int year = date.getYear();
		int DaysInMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		if (month == 1) {
			if (IsLeapYear(year + 1900))
				DaysInMonth[1] = DaysInMonth[1] + 1;
		}
		return DaysInMonth[month];
	}

	public static void main(String[] args) {
		List<Frequency> fs = getFrequencies();
		for (Frequency f : fs) {
			System.out.println(f.getFrequency() + ":" + f.getShowFrequency());
		}

		List<DateWeek> ws = getDateWeeks();
		for (DateWeek w : ws) {
			System.out.println("  " + w.getDateWeek() + ":"
					+ w.getShowDateWeek());
		}

		List<DateSeq> ss = getDateSeqs();
		for (DateSeq s : ss) {
			System.err.println("    " + s.getDateSeq() + ":"
					+ s.getShowDateSeq());
		}

		List<DateValue> vs = getDateValues();
		for (DateValue v : vs) {
			System.out.println("      " + v.getDateValue() + ":"
					+ v.get("showDateValue"));
		}
	}
}
